﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
using System.Web;

namespace BBS.Bll.Cache
{
    public  class  CacheProvider
    {
        private CacheProvider(){}
        private static volatile System.Web.Caching.Cache _webCache = HttpRuntime.Cache;
        /// <summary>
        /// 将对象添加到缓存中
        /// </summary>
        /// <param name="objId">对象ID</param>
        /// <param name="obj">被缓存对象</param>
        /// <param name="expire">过期时间以秒为单位</param>
        public static void AddObject(string objId,object obj,int expire)
        {
            if (!string.IsNullOrEmpty(objId))
            {
                //永久
                DateTime dt = DateTime.MaxValue;
                if (expire>0)
                {
                    dt = DateTime.Now.AddSeconds(expire);
                }
                _webCache.Insert(objId, obj, null, dt, System.Web.Caching.Cache.NoSlidingExpiration);
            }
        }
        /// <summary>
        /// 将对象添加到缓存中，过期时间为永久
        /// </summary>
        /// <param name="objId">对象ID</param>
        /// <param name="obj">被缓存对象</param>
        public static  void AddObject(string objId,object obj)
        {
            //永久
            AddObject(objId, obj, -1);
        }
        /// <summary>
        /// 获取被缓存对象
        /// </summary>
        /// <param name="objId">key</param>
        /// <returns>被缓存对象</returns>
        public static object GetObject(string objId)
        {
            return  _webCache.Get(objId);
        }
        /// <summary>
        /// 移除指定的缓存对象
        /// </summary>
        /// <param name="objId">对象KEY</param>
        public static void Remove(string objId)
        {
            _webCache.Remove(objId);
        }
    }
}
